/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ua.pp.bizon.cripto.gui;

import java.io.IOException;

import javax.swing.JOptionPane;

import ua.pp.bizon.cripto.authorization.Credentials;
import ua.pp.bizon.cripto.configuration.Configuration;
import ua.pp.bizon.cripto.keystore.CryptoException;
import ua.pp.bizon.cripto.keystore.CryptoUtil;
import ua.pp.bizon.cripto.keystore.CryptoUtil.Direction;

/**
 * 
 * @author roman
 */
public class MainFrame extends javax.swing.JFrame {

    private static final long serialVersionUID = -48580509051411815L;

    private Credentials credentials;
    
    private Direction direction;
    
    public Direction getDirection() {
        return direction;
    }
    
    public void setDirection(Direction direction) {
        this.direction = direction;
    }

    public Credentials getCredentials() {
        return credentials;
    }

    public void setCredentials(Credentials credentials) {
        this.credentials = credentials;
    }

    /**
     * Creates new form MainFrame
     * 
     * @throws IOException
     * @throws CryptoException
     */
    public MainFrame(){
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed"
    // desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        selectFromPathPanel = new ua.pp.bizon.cripto.gui.SelectPathPanel();
        fromLabel = new javax.swing.JLabel();
        toLabel = new javax.swing.JLabel();
        selectToPathPanel = new ua.pp.bizon.cripto.gui.SelectPathPanel();
        decodeButton = new javax.swing.JButton();
        encodeButton = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        fromLabel.setText("From;");

        toLabel.setText("to:");

        decodeButton.setText("Decode");
        decodeButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                decodeButtonActionPerformed(evt);
            }
        });

        encodeButton.setText("Encode");
        encodeButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                encodeButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(layout
                .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup().addGap(15, 15, 15).addComponent(fromLabel))
                .addGroup(
                        layout.createSequentialGroup()
                                .addContainerGap()
                                .addGroup(
                                        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                                .addGroup(layout.createSequentialGroup().addGap(6, 6, 6).addComponent(toLabel))
                                                .addComponent(selectFromPathPanel, javax.swing.GroupLayout.PREFERRED_SIZE,
                                                        javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGroup(
                        layout.createSequentialGroup()
                                .addContainerGap()
                                .addComponent(selectToPathPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE,
                                        javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGroup(
                        javax.swing.GroupLayout.Alignment.TRAILING,
                        layout.createSequentialGroup().addContainerGap().addComponent(encodeButton)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED).addComponent(decodeButton).addGap(17, 17, 17)));
        layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(
                layout.createSequentialGroup()
                        .addGap(9, 9, 9)
                        .addComponent(fromLabel)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(selectFromPathPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE,
                                javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(toLabel)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(selectToPathPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE,
                                javax.swing.GroupLayout.PREFERRED_SIZE).addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE).addComponent(decodeButton).addComponent(encodeButton))
                        .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)));

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void decodeButtonActionPerformed(java.awt.event.ActionEvent evt) {// GEN-FIRST:event_decodeButtonActionPerformed
        direction = Direction.DECODE;
        process();
            }// GEN-LAST:event_decodeButtonActionPerformed

    protected void process() {
        try {
            new CryptoUtil(credentials).process(new Configuration(selectFromPathPanel.getSelected(), selectToPathPanel.getSelected(), direction));
        } catch (CryptoException e) {
            JOptionPane.showMessageDialog(this, e.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
        }
    }

    private void encodeButtonActionPerformed(java.awt.event.ActionEvent evt) {// GEN-FIRST:event_encodeButtonActionPerformed
        direction = Direction.ENCODE;
        process();
       
    }// GEN-LAST:event_encodeButtonActionPerformed

    /**
     * @param args
     *            the command line arguments
     */
    public static void main(String args[]) {
        /*
         * Set the Nimbus look and feel
         */
        // <editor-fold defaultstate="collapsed"
        // desc=" Look and feel setting code (optional) ">
        /*
         * If Nimbus (introduced in Java SE 6) is not available, stay with the
         * default look and feel. For details see
         * http://download.oracle.com/javase
         * /tutorial/uiswing/lookandfeel/plaf.html
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(MainFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(MainFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(MainFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(MainFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        // </editor-fold>

        /*
         * Create and display the form
         */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                try {
                    MainFrame frame = new MainFrame();
                    frame.credentials = new Credentials();
                    new UIDialog(null, true).login(frame.credentials);
                    frame.setVisible(true);
                } catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton decodeButton;
    private javax.swing.JButton encodeButton;
    private javax.swing.JLabel fromLabel;
    private ua.pp.bizon.cripto.gui.SelectPathPanel selectFromPathPanel;
    private ua.pp.bizon.cripto.gui.SelectPathPanel selectToPathPanel;
    private javax.swing.JLabel toLabel;

    // End of variables declaration//GEN-END:variables

    public ua.pp.bizon.cripto.gui.SelectPathPanel getSelectFromPathPanel() {
        return selectFromPathPanel;
    }
    
    public void setSelectFromPathPanel(ua.pp.bizon.cripto.gui.SelectPathPanel selectFromPathPanel) {
        this.selectFromPathPanel = selectFromPathPanel;
    }

    public ua.pp.bizon.cripto.gui.SelectPathPanel getSelectToPathPanel() {
        return selectToPathPanel;
    }
    
    public void setSelectToPathPanel(ua.pp.bizon.cripto.gui.SelectPathPanel selectToPathPanel) {
        this.selectToPathPanel = selectToPathPanel;
    }

    
}
